package com.youdianstar.app.util.biometric;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import com.youdianstar.app.R;

import java.util.concurrent.Executor;

/**
 * File: BiometricPopUtil
 * Author: 82149 Create: 2021/7/27 17:07
 * Changes (from 2021/7/27)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 */
public class BiometricPopUtil {
    public static BiometricPrompt createBiometricPrompt(AppCompatActivity activity,IBiometricPopListener listener){
        Executor executor = ContextCompat.getMainExecutor(activity);
        BiometricPrompt.AuthenticationCallback callback = new BiometricPrompt.AuthenticationCallback(){
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                listener.onAuthFailed(errString.toString());
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                listener.onAuthFailed("");
            }

            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                listener.onAuthSuccess(result);
            }
        };
        return new BiometricPrompt(activity,executor,callback);
    }

    public static BiometricPrompt.PromptInfo createPromptInfo(AppCompatActivity activity){
        BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle(activity.getString(R.string.Set_biometric_login))
                .setSubtitle("")
                .setNegativeButtonText(activity.getString(R.string.public_cancel))
                .setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
                .setConfirmationRequired(false)
                .build();

        return  promptInfo;
    }
}
